package com.jwds.digital.bios.business.service.service.dispatch.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchFeedReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.assembler.DispatchFeedAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.assembler.DispatchInternalTransportAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.BasicStockBinReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.feign.FlowBasicReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.entity.dispatch.DispatchFeed;
import com.jwds.digital.bios.business.core.infrastructure.entity.dispatch.DispatchInternalTransport;
import com.jwds.digital.bios.business.core.infrastructure.mapper.dispatch.DispatchFeedMapper;
import com.jwds.digital.bios.business.core.infrastructure.mapper.dispatch.DispatchInternalTransportMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchFeedRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchInternalTransportRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.FlowBasicRespVO;
import com.jwds.digital.bios.business.core.utils.FeignResultUtil;
import com.jwds.digital.bios.business.rpc.feign.BasicStockBinFeign;
import com.jwds.digital.bios.business.rpc.feign.FlowBasicFeign;
import com.jwds.digital.bios.business.service.service.dispatch.IDispatchFeedService;
import com.jwds.tdc.framework.application.lib.BaseResponse;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供料调度实现类
 *
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class DispatchFeedServiceImpl extends ServiceImpl<DispatchFeedMapper, DispatchFeed> implements IDispatchFeedService {

    @Autowired
    private DispatchFeedMapper baseMapper;
    @Autowired
    private BasicStockBinFeign basicStockBinFeign;
    @Autowired
    private FlowBasicFeign flowBasicFeign;

    @Override
    public DispatchFeedRespVO queryById(Long id) {
        if (id == null) {
            return null;
        }
        DispatchFeed entity = baseMapper.selectById(id);
        return DispatchFeedAssembler.entity2dto(entity);
    }

    @Override
    public PageResponse<DispatchFeedRespVO> page(PageRequest<DispatchFeedReqDTO> request) {
        DispatchFeedReqDTO condition = request.getQueryCondition();
        Page<DispatchFeedRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<DispatchFeedRespVO> pageData = baseMapper.pageQuery(page, condition);
        return new PageResponse<>((Page<?>) pageData, pageData.getRecords());
    }

    @Override
    public List<DispatchFeedRespVO> list(DispatchFeedReqDTO reqDto) {
        return baseMapper.queryList(reqDto);
    }

    @Override
    public void delete(Long id) {
        DispatchFeed oldEntity = baseMapper.selectById(id);
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        baseMapper.deleteById(id);
    }

    @Override
    public List<DispatchFeedRespVO> listBlastFurnace(String binName) {
        BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
        basicStockBinReqDTO.setBinName(binName);
        //查询指定的料仓
        List<BasicStockBinRespVO> listBaseStockBin = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
       //根据料仓编码查询指定的料仓
        LambdaQueryWrapper<DispatchFeed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchFeed::getBinCode,listBaseStockBin.get(0).getBinCode());
        List<DispatchFeed> dispatchFeedList = baseMapper.selectList(queryWrapper);
        ArrayList<DispatchFeedRespVO> dispatchFeedReqDTOList = new ArrayList<>();
        for (DispatchFeed dispatchFeed : dispatchFeedList){
            DispatchFeedRespVO dispatchFeedRespVO = new DispatchFeedRespVO();
            BeanUtils.copyProperties(dispatchFeed,dispatchFeedRespVO);
            dispatchFeedReqDTOList.add(dispatchFeedRespVO);
        }
        return dispatchFeedReqDTOList;
    }

    @Override
    public List<DispatchFeedRespVO> blastFurnace(String binName) {
        BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
        basicStockBinReqDTO.setBinName(binName);
//      指定高炉的料仓
        List<BasicStockBinRespVO> listBaseStockBin = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        //根据料仓编码查询指定的料仓
        LambdaQueryWrapper<DispatchFeed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchFeed::getBinCode,listBaseStockBin.stream().findFirst().get().getBinCode());
        List<DispatchFeed> dispatchFeedList = baseMapper.selectList(queryWrapper);
        ArrayList<DispatchFeedRespVO> dispatchFeedRespVOList = new ArrayList<>();
        for (DispatchFeed dispatchFeed : dispatchFeedList) {
            DispatchFeedRespVO dispatchFeedRespVO = new DispatchFeedRespVO();
            BeanUtils.copyProperties(dispatchFeed,dispatchFeedRespVO);
            dispatchFeedRespVOList.add(dispatchFeedRespVO);
        }
        ArrayList<DispatchFeedRespVO> dispatchFeedRespVOS = new ArrayList<>();

        for (DispatchFeedRespVO feedRespVO : dispatchFeedRespVOList) {
            if (feedRespVO.getOriginDevice().equals("QTCPC")){
                dispatchFeedRespVOS.add(feedRespVO);
            } else if (feedRespVO.getOriginDevice().equals("34SJCPC")) {
                dispatchFeedRespVOS.add(feedRespVO);
            } else {
                dispatchFeedRespVOS.add(feedRespVO);
            }
        }
        return dispatchFeedRespVOS;
    }

    @Override
    public List<String> StartingPoint(String originPointion) {
        if (originPointion== null){
            throw new RuntimeException();
        }
        ArrayList<String> arrayList = new ArrayList<>();
        LambdaQueryWrapper<DispatchFeed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchFeed::getOriginPointion,originPointion);
        List<DispatchFeed> list = baseMapper.selectList(queryWrapper);
        for (DispatchFeed datum : list) {
            arrayList.add(datum.getOriginPointion());
        }
        return arrayList;
    }

    @Override
    public List<String> destDevice(String originDevice) {
        if (originDevice== null){
            throw new RuntimeException();
        }
        ArrayList<String> arrayList = new ArrayList<>();
        LambdaQueryWrapper<DispatchFeed> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DispatchFeed::getOriginDevice,originDevice);
        List<DispatchFeed> list = baseMapper.selectList(queryWrapper);
        for (DispatchFeed datum : list) {
            arrayList.add(datum.getDestDevice());
        }
        return arrayList;
    }

    @Override
    public List<String> originDevice(DispatchFeedReqDTO reqDTO) {
        if (reqDTO.getProcessRoute() == null){
            throw new RuntimeException();
        }
        DispatchFeed entity = baseMapper.selectById(reqDTO.getId());
//      通过起点位置的名称获取起点设备的编码
        BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
        basicStockBinReqDTO.setBinName(entity.getOriginPointion());
        List<FlowBasicReqDTO> result =  new ArrayList<>();
        List<BasicStockBinRespVO> binName = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : binName) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setStartDeviceCode(datum.getDeviceOut());
            result.add(dto);
        }
//        通过终点设备的名称获取起点设备的编码
        basicStockBinReqDTO.setBinCode(reqDTO.getDestPointion());
        List<BasicStockBinRespVO> binCode = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : binCode) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setEndDeviceCode(datum.getDeviceIn());
            result.add(dto);
        }
        BaseResponse<List<FlowBasicRespVO>> all = flowBasicFeign.all((FlowBasicReqDTO) result);
        List<FlowBasicRespVO> data = all.getData();
        List<String> list = new ArrayList<>();
        for (FlowBasicRespVO datum : data){
            list.add(datum.getFlowName());
        }
        return list;
    }

    @Override
    public DispatchFeedRespVO execute(DispatchFeedReqDTO reqDTO) {
        DispatchFeed entity = baseMapper.selectById(reqDTO.getId());
//      通过起点位置的名称获取起点设备的编码
        BasicStockBinReqDTO basicStockBinReqDTO = new BasicStockBinReqDTO();
        basicStockBinReqDTO.setBinName(entity.getOriginPointion());
        List<FlowBasicReqDTO> result =  new ArrayList<>();
        List<BasicStockBinRespVO> binName = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : binName) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setStartDeviceCode(datum.getDeviceOut());
            result.add(dto);
        }
//        通过终点设备的名称获取起点设备的编码
        basicStockBinReqDTO.setBinCode(entity.getDestPointion());
        List<BasicStockBinRespVO> binCode = FeignResultUtil.getDataFromBaseResponse(basicStockBinFeign.list(basicStockBinReqDTO));
        for (BasicStockBinRespVO datum : binCode) {
            FlowBasicReqDTO dto = new FlowBasicReqDTO();
            dto.setEndDeviceCode(datum.getDeviceIn());
            result.add(dto);
        }
        BaseResponse<List<FlowBasicRespVO>> all = flowBasicFeign.all((FlowBasicReqDTO) result);
        List<FlowBasicRespVO> data = all.getData();
        List<String> list = data.stream().map(FlowBasicRespVO::getFlowName).collect(Collectors.toList());
        entity.setProcessRoute(list.toString());
        baseMapper.updateById(entity);
        DispatchFeedRespVO dispatchFeedRespVO = new DispatchFeedRespVO();
        BeanUtils.copyProperties(entity,dispatchFeedRespVO);
        return dispatchFeedRespVO;
    }

}
